/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.web.controller;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.dto.JsonPageResponseDto;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * Created by Peter.Li on 2015/10/8.
 */
@Controller
//@RequestMapping(value = "/")
public class GlobalController {

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return "index";
    }

    @RequestMapping(value = "/dashboard", method = RequestMethod.GET)
    public String tag(Model model) {
        return "dashboard";
    }

    @RequestMapping(value = "/chartView", method = RequestMethod.GET)
    public String report(Model model) {
        return "report/chartView";
    }

    @RequestMapping(value = "/help", method = RequestMethod.GET)
    public String help(Model model) {
        return "help";
    }

    @RequestMapping(value = "/lang", method = RequestMethod.GET)
    public String lang(HttpServletRequest request, HttpServletResponse response, Model model,
                       @RequestParam(value = "lang", defaultValue = "en") String lang) {
        if (!model.containsAttribute("contentModel")) {
            Locale locale = new Locale("en", "US");
            (new CookieLocaleResolver()).setLocale(request, response, locale);
//            if (lang.equals("zh")) {
//                Locale locale = new Locale("zh", "CN");
//                (new CookieLocaleResolver()).setLocale(request, response, locale);
//            } else if (lang.equals("en")) {
//                Locale locale = new Locale("en", "US");
//                (new CookieLocaleResolver()).setLocale(request, response, locale);
//            } else {
//                (new CookieLocaleResolver()).setLocale(request, response, LocaleContextHolder.getLocale());
//            }
//            RequestContext requestContext = new RequestContext(request);
//            model.addAttribute("money", requestContext.getMessage("money"));
        }
        return "redirect:/";
    }

    @RequestMapping(value = "/release", method = RequestMethod.GET)
    public String release(Model model) { return "release"; }


    @ResponseBody
    @RequestMapping(value = "/empty", method = RequestMethod.POST)
    public JsonPageResponseDto empty(Model model, ServletRequest request) {
        JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(Lists.newArrayList());
        json.setRecordsTotal(0);
        json.setRecordsFiltered(0);
        json.setDraw(0);
        return json;
    }
}
